package com.airsystem.pos.bengkel.controller;

import java.math.BigDecimal;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.entity.Karyawan;
import com.airsystem.pos.bengkel.interfaces.IMaster;
import com.airsystem.pos.bengkel.model.MasterEmployeeModel;
import com.airsystem.pos.bengkel.service.KaryawanServices;
import com.airsystem.pos.bengkel.view.dialog.MasterEmployeeDialog;

/**
 * @author Budi Oktaviyan <budi.oktaviyan@icloud.com>
 */
public class MasterEmployeeController extends MasterEmployeeModel implements IMaster {
	private static final Logger LOG = Logger.getLogger(MasterEmployeeController.class.getSimpleName());

	private String nama, gaji, uangMakan, id;

	private KaryawanServices karyawanServices = new KaryawanServices();

	public MasterEmployeeController() {
		super();
	}

	public void setup() {
		updateButton.setVisible(false);
		deleteButton.setVisible(false);
		idField.setVisible(false);
	}

	public void search() {
		dialogMasterEmployee = new MasterEmployeeDialog();

		try {
			if (dialogMasterEmployee.getNama() == null
			||  dialogMasterEmployee.getGaji() == null
			||  dialogMasterEmployee.getUangMakan() == null) {
				cancel();
			} else {
				namaField.setText(dialogMasterEmployee.getNama());
				gajiField.setText(dialogMasterEmployee.getGaji());
				uangmakanField.setText(dialogMasterEmployee.getUangMakan());
				idField.setText(dialogMasterEmployee.getId());

				saveButton.setVisible(false);
				updateButton.setVisible(true);
				deleteButton.setVisible(true);
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}

	public void get() {
		nama = namaField.getText();
		gaji = gajiField.getText();
		uangMakan = uangmakanField.getText();
		id = idField.getText();
	}

	public void reset() {
		namaField.setText("");
		gajiField.setText("");
		uangmakanField.setText("");
		idField.setText("");
	}

	public void save() {
		try {
			get();

			if (nama.isEmpty() || gaji.isEmpty() || uangMakan.isEmpty()) {
				JOptionPane.showMessageDialog(MasterEmployeeController.this, "Data kosong!", "Pesan", JOptionPane.WARNING_MESSAGE);
			} else {
				Karyawan karyawan = new Karyawan();
				karyawan.setNama(nama);
				karyawan.setGaji(new BigDecimal(gaji));
				karyawan.setUang_makan(new BigDecimal(uangMakan));

				karyawanServices.saveOrUpdate(karyawan);

				JOptionPane.showMessageDialog(MasterEmployeeController.this, "Simpan data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
				reset();
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(MasterEmployeeController.this, "Simpan data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void update() {
		try {
			get();

			if (nama.isEmpty() || gaji.isEmpty() || uangMakan.isEmpty() || id.isEmpty()) {
				JOptionPane.showMessageDialog(MasterEmployeeController.this, "Data kosong!", "Pesan", JOptionPane.WARNING_MESSAGE);
			} else {
				Karyawan karyawan = new Karyawan();
				karyawan.setId(Long.valueOf(id));
				karyawan.setNama(nama);
				karyawan.setGaji(new BigDecimal(gaji));
				karyawan.setUang_makan(new BigDecimal(uangMakan));

				karyawanServices.saveOrUpdate(karyawan);

				JOptionPane.showMessageDialog(MasterEmployeeController.this, "Ubah data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
				cancel();
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(MasterEmployeeController.this, "Ubah data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void delete() {
		try {
			get();

			if (nama.isEmpty() || gaji.isEmpty() || uangMakan.isEmpty() || id.isEmpty()) {
				JOptionPane.showMessageDialog(MasterEmployeeController.this, "Data kosong!", "Pesan", JOptionPane.WARNING_MESSAGE);
			} else {
				Karyawan karyawan = new Karyawan();
				karyawan.setId(Long.valueOf(id));
				karyawan.setNama(nama);
				karyawan.setGaji(new BigDecimal(gaji));
				karyawan.setUang_makan(new BigDecimal(uangMakan));

				karyawanServices.delete(karyawan);

				JOptionPane.showMessageDialog(MasterEmployeeController.this, "Hapus data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
				cancel();
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(MasterEmployeeController.this, "Hapus data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void cancel() {
		reset();

		try {
			saveButton.setVisible(true);
			updateButton.setVisible(false);
			deleteButton.setVisible(false);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}
}